package leetcode;


import java.util.*;

/**
 * 钥匙和房间
 */
public class LeetCode841 {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        if (rooms == null || rooms.size() == 0) return false;
        Set<Integer> visited = new HashSet<>();
        Stack<Integer> stack = new Stack<>();
        for (Integer key : rooms.get(0)) stack.push(key);
        visited.add(0);
        while (!stack.isEmpty()) {
            Integer room = stack.pop();
            visited.add(room);
            for (Integer key : rooms.get(room)) if (!visited.contains(key)) stack.push(key);
        }
        return visited.size() == rooms.size();
    }
}